package casamento.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import casamento.Bean.ImagenBean;

public class ImagemDAO {

	Connection connection;

	public void inserirImagem(ImagenBean imgArray) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction t = session.beginTransaction();
		session.save(imgArray);
		t.commit();

	}
	
	public void inserirImagemPorID(ImagenBean imgArray) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction t = session.beginTransaction();
		session.save(imgArray);
		t.commit();

	}

	public List<ImagenBean> getImagem(Integer id)
			throws ClassNotFoundException, SQLException {
		this.connection = new Conexao().getConnection();

		String sql = "SELECT * FROM banco.TbImagem WHERE empresa_id = ?";
		List<ImagenBean> lista = new ArrayList<ImagenBean>();
		
		PreparedStatement stmt = connection.prepareStatement(sql);
		stmt.setInt(1, id);
		ResultSet rs = stmt.executeQuery();

		while (rs.next()) {
			
			ImagenBean img = new ImagenBean();
			img.setId(rs.getInt("id"));
			img.setArquivo(rs.getBytes("file"));
			img.setNome(rs.getString("nome"));
			lista.add(img);

		}

		return lista;

	}

}
